package com.cxb.demotest.mysqltest;

import com.alibaba.fastjson.JSONObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

/**
 * @author Xbao
 * @description
 * @date 2022/6/17 9:44
 */

public class TestMysql {
  public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection(
        "10.22.1.109:3306/test2_epidemic_cq?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&emptyStringsConvertToZero=true&serverTimezone=GMT%2B8",
        "yunsom_scm",
        "T0NqKo42lK");
    PreparedStatement pstm = connection.prepareStatement(
        "select * from test_table",
        ResultSet.TYPE_FORWARD_ONLY,
        ResultSet.CONCUR_READ_ONLY
    );
    pstm.setFetchSize(Integer.MIN_VALUE);
    ResultSet resultSet = pstm.executeQuery();
    int columnCount;
    JSONObject jsonObject;
    while (resultSet.next()) {
      ResultSetMetaData metaData = resultSet.getMetaData();
      jsonObject = new JSONObject();
      columnCount = metaData.getColumnCount();
      for (int m = 1; m <= columnCount; m++) {
        jsonObject.put(metaData.getColumnName(m), resultSet.getString(m));
      }
      System.out.println(jsonObject);
    }
  }
}
